import Fluent
import Vapor

final class APNSModel: Model, Content {
    static let schema = "apns"
    
    @ID(custom: FieldKeys.id)
    var id: String?
    
    @Field(key: FieldKeys.token)
    var token: String
    
    @Field(key: FieldKeys.locationPushToken)
    var locationPushToken: String
    
    @Field(key: FieldKeys.alias)
    var alias: String
    
    @Field(key: FieldKeys.tags)
    var tags: [String]
    
    @Field(key: FieldKeys.deviceID)
    var deviceID: String
    
    @Timestamp(key: FieldKeys.createdAt, on: .create)
    var createdAt: Date?
    
    @Timestamp(key: FieldKeys.updatedAt, on: .update)
    var updatedAt: Date?
    
    init(id: String? = nil, token: String, locationPushToken: String, alias: String, tags: [String], deviceID: String, createdAt: Date? = nil, updatedAt: Date? = nil) {
        self.id = id
        self.token = token
        self.locationPushToken = locationPushToken
        self.alias = alias
        self.tags = tags
        self.deviceID = deviceID
        self.createdAt = createdAt
        self.updatedAt = updatedAt
    }
    init() {}
//    
//    init(token: String, alias: String, tags: [String], deviceID: String) {
//        self.id = UUID()
//        self.token = token
//        self.alias = alias
//        self.tags = tags
//        self.deviceID = deviceID
//    }
}

extension APNSModel {
    struct FieldKeys {
        static var id: FieldKey { "apns_id" }
        static var token: FieldKey { "token" }
        static var locationPushToken: FieldKey { "location_push_token" }
        static var alias: FieldKey { "alias" }
        static var tags: FieldKey { "tags" }
        static var deviceID: FieldKey { "device_id" }
        static var createdAt: FieldKey { "created_at" }
        static var updatedAt: FieldKey { "updated_at" }
    }
}

struct APNSModelMigration: AsyncMigration {
    func prepare(on database: Database) async throws {
        try await database.schema(APNSModel.schema)
            .field(APNSModel.FieldKeys.id, .string, .required, .identifier(auto: false), .custom("COMMENT 'APNS 主键'"))
            .field(APNSModel.FieldKeys.token, .string, .required, .custom("COMMENT 'APNS Token'"))
            .field(APNSModel.FieldKeys.locationPushToken, .string, .required, .custom("COMMENT 'location push token'"))
            .field(APNSModel.FieldKeys.alias, .string, .required, .custom("COMMENT '设备别名'"))
            .field(APNSModel.FieldKeys.tags, .array(of: .string), .custom("COMMENT '设备标签'"))
            .field(APNSModel.FieldKeys.deviceID, .string, .required, .custom("COMMENT '设备ID'"))
            .field(APNSModel.FieldKeys.createdAt, .datetime, .custom("COMMENT '创建时间'"))
            .field(APNSModel.FieldKeys.updatedAt, .datetime, .custom("COMMENT '更新时间'"))
            .unique(on: APNSModel.FieldKeys.deviceID)
            .unique(on: APNSModel.FieldKeys.token)
            .create()
    }
    
    func revert(on database: Database) async throws {
        try await database.schema(APNSModel.schema).delete()
    }
}
